/******************************************************************************
 * @File name   :      CardDefineServiceImpl.java
 *
 * @Author      :      JIZHANGS
 *
 * @Date        :      Aug 12, 2015
 *
 * @Copyright Notice: 
 * Copyright (c) 2015 Capgemini, Inc. All  Rights Reserved.
 * This software is published under the terms of the Capgemini Software
 * License version 1.0, a copy of which has been included with this
 * distribution in the LICENSE.txt file.
 * 
 * 
 * ----------------------------------------------------------------------------
 * Date                   Who         Version        Comments
 * Aug 12, 2015 1:39:56 PM        JIZHANGS     1.0            Initial Version
 *****************************************************************************/
package com.cap.bts.crmdp.dealer.card.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.cap.bts.crmdp.dealer.card.constants.CardConstants;
import com.cap.bts.crmdp.dealer.card.dao.ICardCreateDao;
import com.cap.bts.crmdp.dealer.card.dao.ICardDefineDao;
import com.cap.bts.crmdp.dealer.card.domain.CardDefinition;
import com.cap.bts.crmdp.dealer.card.dto.CardCreateResultsDto;
import com.cap.bts.crmdp.dealer.card.dto.CardDefineQueryDto;
import com.cap.bts.crmdp.dealer.card.service.ICardCreateService;
import com.cap.bts.framework.common.exception.AppException;

/**
 *
 */
@Transactional(rollbackFor = AppException.class)
@Service
public class CardCreateServiceImpl implements ICardCreateService {

    @Resource(name = "cardDefineDao")
    private ICardDefineDao cardDefineDao;
    
    @Resource(name = "cardCreateDao")
    private ICardCreateDao cardCreateDao;

    /**
     * {@inheritDoc} 
     * overridden:
     * @Date        :      Aug 19, 2015
     * @see com.cap.bts.crmdp.dealer.card.service.ICardCreateService#findAllCardGenerationList()
    **/
    @Override
    public List<CardCreateResultsDto> findAllCardGenerationList() throws AppException {
        CardDefineQueryDto cdq=new CardDefineQueryDto();
        cdq.setQueryStatus(1);
        List<CardDefinition> list= cardDefineDao.getGenerateDefinitionListByDto(cdq);
        List<CardCreateResultsDto> resultList=null;
        if(list!=null&&!list.isEmpty()){
            resultList=new ArrayList<CardCreateResultsDto>();
            for(int i=0;i<list.size();i++){
                CardCreateResultsDto dto=new CardCreateResultsDto();
                dto.setCardDefinition(list.get(i));
                String cardnumMax=CardConstants.getMaxNum(list.get(i).getCardNum(), list.get(i).getPerfix(),list.get(i).getLengthOfDigital());  
                //如果是最大卡号则显示为已用完
                if(cardnumMax.equals(list.get(i).getCardNum())){
                    dto.setCardNumFrom("已用完(最新卡号："+list.get(i).getCardNum()+")");
                    dto.setUsedUpFlag(true);
                }else{
                    dto.setCardNumFrom(cardCreateDao.getNextCardNumFrom(list.get(i)));
                }
                resultList.add(dto);
            }
        }
        return resultList;
    }

    /**
     * {@inheritDoc} 
     * overridden:
     * @Date        :      Aug 19, 2015
     * @see com.cap.bts.crmdp.dealer.card.service.ICardCreateService#getNextCardNumFrom(java.lang.Integer, java.lang.Integer)
    **/
    @Override
    public String getCardNumTo(Long id, Integer quantity)
        throws AppException {
        CardDefinition  cd=cardDefineDao.findById(id);
        cardDefineDao.clear();
        if(cd!=null){
            return cardCreateDao.getNextCardNumTo(cd,quantity);
        }
        return null;
    }

    /**
     * {@inheritDoc} 
     * overridden:
     * @Date        :      Aug 20, 2015
     * @see com.cap.bts.crmdp.dealer.card.service.ICardCreateService#isCardNameExsit(java.lang.Long, java.lang.String)
    **/
    @Override
    public Boolean isCardNameExsit(Long id, String name) throws AppException {
        List<CardDefinition> list=cardDefineDao.getListByQuery("from CardDefinition where name='"+name+"'");
        if(list!=null && !list.isEmpty()){
            return true;
        }
        return false;
    }

    /**
     * {@inheritDoc} 
     * overridden:
     * @Date        :      Aug 21, 2015
     * @see com.cap.bts.crmdp.dealer.card.service.ICardCreateService#getGenerateQuantity(java.lang.String, java.lang.Integer)
    **/
    @Override
    public Integer getGenerateQuantity(Long id, Integer quantity) throws AppException {
        Integer num=quantity;
        CardDefinition  cd=cardDefineDao.findById(id);
        cardDefineDao.clear();
        String cardnum=CardConstants.getCardToNum(cd.getCardNum(), cd.getPerfix(),cd.getLengthOfDigital(), cd.getCheckBitEnabeld(), num);
        String cardnumMax=CardConstants.getMaxNum(cd.getCardNum(), cd.getPerfix(),cd.getLengthOfDigital());
        if(cardnum.compareTo(cardnumMax)>0||cardnum.length()>cardnumMax.length()){
            Integer endNum=Integer.valueOf(cardnumMax.substring(cd.getPerfix().length(), cardnumMax.length()));
            Integer startNum=Integer.valueOf(cd.getCardNum().substring(cd.getPerfix().length(), cardnumMax.length()));
            num=endNum-startNum;
        }
        return num;
    }

    /**
     * {@inheritDoc} 
     * overridden:
     * @Date        :      Aug 22, 2015
     * @see com.cap.bts.crmdp.dealer.card.service.ICardCreateService#generateCard(java.lang.String)
    **/
    @Override
    public Boolean generateCard(String generateInfo) throws AppException {
        return cardCreateDao.generateCard(generateInfo);
    }
   
}
